﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;
using Common.Extension;

//同花顺主题收集
namespace HotTopic.collector
{
    class ThsCollector : AbstractCollector
    {
//        private static void Main(string[] args)
//        {
//            ThsCollector collector = new ThsCollector();
//            var other = collector.GetOtherTopicMap();
//            Console.ReadKey();
//        }

        private static readonly String ThsPath = Config.ThsPath;

        protected override Dictionary<string, List<string>> GetIndustryMap()
        {
            return null;
        }

        protected override Dictionary<string, List<string>> GetConceptMap()
        {
            String path = ThsPath + @"\system\同花顺方案\StockBlock.ini";
            Dictionary<string, string> tempSector = new Dictionary<string, string>();
            Dictionary<string, string> tempNameMap = new Dictionary<string, string>();
            Dictionary<string, string> tempBlockContext = new Dictionary<string, string>();
            Dictionary<string, List<string>> tempCodeName = new Dictionary<string, List<string>>();
            Dictionary<string, List<string>> tempBlockStk = new Dictionary<string, List<string>>();
            using (StreamReader sr = new StreamReader(path, Encoding.Default))
            {
                string strstk = "";
                while (!sr.EndOfStream)
                {
                    string tempstr = sr.ReadLine();
                    //获取板块的代码
                    //概念:[@8],行业:[@1001],地区:[@9]
                    if (tempstr.StartsWith("[@8]") || tempstr.StartsWith("[@9]") || tempstr.StartsWith("[@1001]"))
                    {
                        while ((tempstr = sr.ReadLine()) != "")
                        {
                            string[] temp = tempstr.Split('=');
                            tempSector.Add(temp[0], temp[1]);
                        }
                    }
                    //记录所有section
                    if (tempstr.StartsWith("[@"))
                    {
                        string tempcode = tempstr.Substring(1, tempstr.Length - 2);
                        tempCodeName.Put(tempcode, new List<string>());

                        while ((strstk = sr.ReadLine()) != "")
                        {
                            tempCodeName[tempcode].Add(strstk.Split('=')[0]);
                        }
                    }
                    //
                    if (tempstr.StartsWith("[BLOCK_NAME_MAP_TABLE]"))
                    {
                        while ((strstk = sr.ReadLine()) != "")
                        {
                            string[] temp = strstk.Split('=');
                            tempNameMap.Add(temp[0], temp[1]);
                        }
                    }

                    if (tempstr.StartsWith("[BLOCK_STOCK_CONTEXT]"))
                    {
                        while ((strstk = sr.ReadLine()) != "")
                        {
                            string[] temp = strstk.Split('=');
                            tempBlockContext.Add(temp[0], temp[1]);
                        }
                    }
                }
            }
            foreach (var temp in tempSector)
            {
                if (temp.Value.StartsWith("@"))
                {
                    foreach (var temp2 in tempCodeName[temp.Value])
                    {
                        if (tempBlockContext.ContainsKey(temp2))
                        {
                            var liststr = tempBlockContext[temp2].Split(',').ToList();
                            liststr.RemoveAt(liststr.Count - 1);
                            var xx = liststr.Select(p => p.Split(':')[1]).ToList()
                                .Select(p => p.Substring(0, 1) == "6" ? p + ".SH" : p + ".SZ").ToList();
                            tempBlockStk.Add(tempNameMap[temp2], xx);
                        }
                    }
                }
                else
                {
                    if (tempBlockContext.ContainsKey(temp.Key))
                    {
                        var liststr = tempBlockContext[temp.Key].Split(',').ToList();
                        liststr.RemoveAt(liststr.Count - 1);
                        var xx = liststr.Select(p => p.Split(':')[1]).ToList()
                            .Select(p => p.Substring(0, 1) == "6" ? p + ".SH" : p + ".SZ").ToList();
                        tempBlockStk.Add(tempNameMap[temp.Key], xx);
                    }
                }
            }
            return tempBlockStk;
        }

        protected override Dictionary<string, List<string>> GetAreaMap()
        {
            return null;
        }

        protected override Dictionary<string, List<string>> GetOtherTopicMap()
        {
            return null;
        }
    }
}